package com.snpic.appaw.module.insure.service.vehicleregistration;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.snpic.appaw.module.insure.controller.app.vehicleregistration.vo.*;
import com.snpic.appaw.module.insure.dal.dataobject.vehicleregistration.VehicleRegistrationDO;
import com.snpic.appaw.framework.common.pojo.PageResult;
import com.snpic.appaw.framework.common.pojo.PageParam;
import com.snpic.appaw.framework.common.util.object.BeanUtils;

import com.snpic.appaw.module.insure.dal.mysql.vehicleregistration.VehicleRegistrationMapper;

import static com.snpic.appaw.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.snpic.appaw.framework.common.util.collection.CollectionUtils.convertList;
import static com.snpic.appaw.framework.common.util.collection.CollectionUtils.diffList;
import static com.snpic.appaw.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static com.snpic.appaw.module.insure.enums.ErrorCodeConstants.*;

/**
 * 车辆登记 Service 实现类
 *
 * @author jijun
 */
@Service
@Validated
public class VehicleRegistrationServiceImpl implements VehicleRegistrationService {

	@Resource
	private VehicleRegistrationMapper vehicleRegistrationMapper;

	/**
	 * 创建车辆登记
	 * 每位用户最多添加5辆车；当用户添加第6辆车时，阻断
	 * @param createReqVO 创建信息
	 * @return 编号
	 */
	@Override
	public Long createVehicleRegistration(AppVehicleRegistrationSaveReqVO createReqVO) {


		// 插入
		VehicleRegistrationDO vehicleRegistration = BeanUtils.toBean(createReqVO, VehicleRegistrationDO.class);
		vehicleRegistrationMapper.insert(vehicleRegistration);

		// 返回
		return vehicleRegistration.getId();
	}

	@Override
	public void updateVehicleRegistration(AppVehicleRegistrationSaveReqVO updateReqVO) {
		// 校验存在
		validateVehicleRegistrationExists(updateReqVO.getId());
		// 更新
		VehicleRegistrationDO updateObj = BeanUtils.toBean(updateReqVO, VehicleRegistrationDO.class);
		vehicleRegistrationMapper.updateById(updateObj);
	}

	@Override
	public void deleteVehicleRegistration(Long id) {
		// 校验存在
		validateVehicleRegistrationExists(id);
		// 删除
		vehicleRegistrationMapper.deleteById(id);
	}

	@Override
	public void deleteVehicleRegistrationListByIds(List<Long> ids) {
		// 删除
		vehicleRegistrationMapper.deleteByIds(ids);
	}


	private void validateVehicleRegistrationExists(Long id) {
		if (vehicleRegistrationMapper.selectById(id) == null) {
			throw exception(VEHICLE_REGISTRATION_NOT_EXISTS);
		}
	}

	@Override
	public VehicleRegistrationDO getVehicleRegistration(Long id) {
		return vehicleRegistrationMapper.selectById(id);
	}

	@Override
	public PageResult<VehicleRegistrationDO> getVehicleRegistrationPage(AppVehicleRegistrationPageReqVO pageReqVO) {
		return vehicleRegistrationMapper.selectPage(pageReqVO);
	}

}